package com.yunxin.admin.system.api.core.customer;

import com.yunxin.admin.system.application.RoleApplicationService;
import com.yunxin.admin.system.domain.model.user.UserMerchantRepository;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.common.constant.SystemConstants;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.common.result.Result;
import com.yunxin.core.admin.domain.AdminUser;
import com.yunxin.core.admin.service.AdminUserService;
import com.yunxin.core.application.service.TeamService;
import com.yunxin.core.domain.model.team.Team;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "13.客户管理下拉")
@Slf4j
@RestController
@RequestMapping("/core/v1/customer/drop")
@RequiredArgsConstructor
public class CustomerDropController {

    private final RoleApplicationService roleService;


    private final TeamService teamService;

    private final UserMerchantRepository userMerchantRepository;
    @Autowired
    private AdminUserService adminUserService;

    @Operation(summary = "业务员或话务员下拉列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/followList")
    public Result<Map<String,List<AdminUser>>> followList(@RequestParam(value = "teamId",required = false) Integer teamId) {
        Map<String,List<AdminUser>> map=new HashMap<>();
        if (ObjectUtils.isEmpty(teamId)){
            return Result.success(map);
        }
        // 获取所有角色为业务员的id
        List<AdminUser> salesmanUsers = roleService.userListByRoleCode(SystemConstants.BOKE_BUSINESS, teamId);
        List<AdminUser> telephonerUsers = roleService.userListByRoleCode(SystemConstants.BOKE_TELEPHONE, teamId);

        map.put("salesmanUsers",salesmanUsers);
        map.put("telephonerUsers",telephonerUsers);
        return Result.success(map);
    }

    @Operation(summary = "通过业务员id查询话务员下拉列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/telephoneListBySalesmanId")
    public Result<List<AdminUser>> telephoneListBySalesmanId(@RequestParam(value = "teamId") Integer teamId,
                                                             @RequestParam(value = "salesmanId") Integer salesmanId) {
        // 获取所有角色为话务的id
        return Result.success(roleService.telephoneListBySalesmanId(SystemConstants.BOKE_TELEPHONE,salesmanId,teamId));
    }

    @Operation(summary = "跟进不同团队查询业务员或话务员下拉列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @GetMapping("/getUserList")
    public Result<Map<String,List<AdminUser>>> getUserList(@RequestParam(value = "teamId",required = false) Integer teamId) {
        Long userId = SecurityUtils.X.getUser().getUserId();
        Map<String,List<AdminUser>> map=new HashMap<>();
        //商户管理员
        List<Integer> teamIds=new ArrayList<>();
        if (!SecurityUtils.X.isRoot()){
            if(SecurityUtils.X.isBokeMerchantUser()) {
                // 获取所有角色为业务员的id
                if (!ObjectUtils.isEmpty(teamId)){
                    teamIds.add(teamId);
                }else {
                    //商户管理员只能看自己所有团队下的账户信息
                    Long merchantId = userMerchantRepository.selectMerchantIdByUserId(userId);
                    List<Team> teams = teamService.getTeamList(merchantId);
                    teamIds = teams.stream().map(i -> i.getId()).toList();
                    if (teamIds.size()<1){
                        Result.success(map);
                    }
                }
                List<AdminUser> salesmanUsers = roleService.getUserList(SystemConstants.BOKE_BUSINESS, teamIds);
                List<AdminUser> telephonerUsers = roleService.getUserList(SystemConstants.BOKE_TELEPHONE, teamIds);

                map.put("salesmanUsers",salesmanUsers);
                map.put("telephonerUsers",telephonerUsers);
                return Result.success(map);
            }else if(SecurityUtils.X.isBokeTeamUser()){
                //团队管理员
                if (!ObjectUtils.isEmpty(teamId)){
                    teamIds.add(teamId);
                }else {
                    //团队管理员只能看自己团队下的账户信息
                    List<Team> teams = teamService.getTeamListByUserId(userId);
                    teamIds = teams.stream().map(i -> i.getId()).toList();
                    if (teamIds.size()<1){
                        Result.success(map);
                    }
                }
                List<AdminUser> salesmanUsers = roleService.getUserList(SystemConstants.BOKE_BUSINESS, teamIds);
                List<AdminUser> telephonerUsers = roleService.getUserList(SystemConstants.BOKE_TELEPHONE, teamIds);

                map.put("salesmanUsers",salesmanUsers);
                map.put("telephonerUsers",telephonerUsers);
                return Result.success(map);
            }else if (SecurityUtils.X.isSalesmanUser()){
                //业务员
                if (!ObjectUtils.isEmpty(teamId)){
                    teamIds.add(teamId);
                }else {
                    //业务员只能看自己团队下的账户信息
                    List<Team> teams = teamService.getTeamListByUserId(userId);
                    teamIds = teams.stream().map(i -> i.getId()).toList();
                    if (teamIds.size()<1){
                        Result.success(map);
                    }
                }
                List<AdminUser> salesmanUsers = new ArrayList<>();
                AdminUser adminUser = adminUserService.getById(userId);
                salesmanUsers.add(adminUser);
                List<AdminUser> telephonerUsers = roleService.getUserList(SystemConstants.BOKE_TELEPHONE, teamIds);
                map.put("salesmanUsers",salesmanUsers);
                map.put("telephonerUsers",telephonerUsers);
                return Result.success(map);
            }else if (SecurityUtils.X.isTelephonerUser()){
                //话务员
                if (!ObjectUtils.isEmpty(teamId)){
                    teamIds.add(teamId);
                }else {
                    //话务员只能看自己团队下的账户信息
                    List<Team> teams = teamService.getTeamListByUserId(userId);
                    teamIds = teams.stream().map(i -> i.getId()).toList();
                    if (teamIds.size()<1){
                        Result.success(map);
                    }
                }
                List<AdminUser> salesmanUsers = roleService.getUserList(SystemConstants.BOKE_BUSINESS, teamIds);
                map.put("salesmanUsers",salesmanUsers);
                List<AdminUser> telephonerUsers = new ArrayList<>();
                AdminUser adminUser = adminUserService.getById(userId);
                telephonerUsers.add(adminUser);
                map.put("telephonerUsers",telephonerUsers);
                return Result.success(map);
            }
        }else {
            if (ObjectUtils.isEmpty(teamId)){
                return Result.success(map);
            }else {
                teamIds.add(teamId);
                List<AdminUser> salesmanUsers = roleService.getUserList(SystemConstants.BOKE_BUSINESS, teamIds);
                List<AdminUser> telephonerUsers = roleService.getUserList(SystemConstants.BOKE_TELEPHONE, teamIds);

                map.put("salesmanUsers",salesmanUsers);
                map.put("telephonerUsers",telephonerUsers);
                return Result.success(map);
            }
        }
        return Result.success(map);
    }


}
